package com.atguigu;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Ticket{
    int ticket = 50; //掉代表票数
    public synchronized void sale(){
     for (int i=1;i<ticket;){
         ticket--;
         System.out.println(Thread.currentThread().getName() + "买到票,剩余票数" + ticket);
     }
    }
}
class TicketLock{
    int ticket = 50; //掉代表票数
    //创建一个锁
   private Lock lock = new ReentrantLock();
    public  void sale(){

            lock.lock();
        try {
            for (int i=0;i<ticket;){
                ticket--;
                System.out.println(Thread.currentThread().getName() + "买到票,剩余票数" + ticket);
            }
        } finally {
            //lock .unlock();
        }

    }
}

/**
 * 三个售票员    卖出      50张票
 * */
public class Test {
    public static void main(String[] args) {
       /* MyThread myThread = new MyThread();
        Thread thread = new Thread(myThread, "售票员1");
        Thread thread1 = new Thread(myThread, "售票员2");
        Thread thread2 = new Thread(myThread, "售票员3");
        //开启线程
        thread.start();
        thread1.start();
        thread2.start();*/
        List<String> strings = new ArrayList<>();
        //企业级开发代码 高内聚第耦合
        Ticket ticket = new Ticket();
        TicketLock ticketLock = new TicketLock();
        //使用lambda表达式
        new Thread(() -> { ticketLock.sale(); },"a").start();
        new Thread(() -> { ticketLock.sale(); },"b").start();
        new Thread(() -> { ticketLock.sale(); },"c").start();

        /*new Thread(new Runnable() {
            @Override
            public void run() {
                ticket.sale();
            }
        },"b").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                ticket.sale();
            }
        },"c").start();*/

    }

}
